import {upLoadImg} from '@/api/upload'

export function convertImgToBase64(url,name,callback,outputFormat){
    var canvas = document.createElement('CANVAS'); 
    var ctx = canvas.getContext('2d'); 
    var img = new Image; 
    img.src = url;
    img.crossOrigin = 'Anonymous'; 
    img.onload = function(){
        var width = img.width;
        var height = img.height;
        // 按比例压缩2倍
        // var rate = (width<height ? width/height : height/width)/2;
        canvas.width = width; 
        canvas.height = height; 
        ctx.drawImage(img,0,0,width,height,0,0,width,height); 
        var dataURL = canvas.toDataURL(outputFormat || 'image/png');  
        canvas = null;
        dataURL = dataURL.split(',')[1];
        let callObj = {flag:false};
        upLoadImg(dataURL,name).then((res) => { 
            callObj.flag = true
            callback.call(this,callObj); 
        }).catch(() => {
            callback.call(this,callObj); 
        })
    }
}